package com.example.productmanagerspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.productmanagerspringboot.data.entity.MyUser;
import com.example.productmanagerspringboot.data.pojo.LoginUser;
import com.example.productmanagerspringboot.data.vo.Response;
import com.example.productmanagerspringboot.data.vo.ResponseWithData;
import com.example.productmanagerspringboot.data.vo.UserVO;
import com.example.productmanagerspringboot.mapper.UserMapper;
import com.example.productmanagerspringboot.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "用户")
public class UserController {
    private final LoginService loginService;

    public UserController(LoginService loginService) {
        this.loginService = loginService;
    }

    @Operation(summary = "登录", description = "使用账号密码登录")
    @PostMapping("/login")
    public ResponseEntity<ResponseWithData<UserVO>> login(@RequestBody MyUser myUser) {
        ResponseWithData<UserVO> response = loginService.login(myUser);

//        if (u == null) {
//            response.setResultCode(500);
//            response.setMessage("登录失败,用户名或密码错误");
//        } else {
//            response.setResultCode(200);
//            response.setMessage("登录成功");
//            response.setData(new UserVO(, u.getType()));
//        }
        return ResponseEntity.ok().body(response);
    }

    @GetMapping("/logout")
    public ResponseEntity<Response> logout() {
        Response response = new Response();
        if (!loginService.logout()) {
            response.setResultCode(500);
            response.setMessage("登录失败,用户名或密码错误");
        } else {

            response.setResultCode(200);
            response.setMessage("登录成功");
        }
        return ResponseEntity.ok().body(response);
    }

    @PostMapping("/register")
    public ResponseEntity<Response> register(@RequestBody MyUser myUser) {
        Response response = new Response();
        if (loginService.register(myUser)) {
            response.setResultCode(500);
            response.setMessage("注册失败，已有该账号");
        } else {
            response.setResultCode(200);
            response.setMessage("注册成功");
        }
        return ResponseEntity.ok().body(response);
    }

    @GetMapping("/info")
    public ResponseEntity<ResponseWithData<LoginUser>> info(HttpServletRequest httpServletRequest) {
        ResponseWithData<LoginUser> response = new ResponseWithData<>();
        response.setResultCode(200);
        response.setMessage("获取成功");
//            int i = Integer.parseInt(token);
//            MyUser myUser = mapper.selectById(i);
        response.setData(loginService.info());
        return ResponseEntity.ok().body(response);
    }

}
